package de.haw.shc;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

import android.widget.ToggleButton;
import de.haw.shc.Action.Action_Impl;
import de.haw.shc.Config.XML_Reader;
import de.haw.shc.Device.DeviceActivity;
import de.haw.shc.Interfaces.DeviceProperty.Device;
import de.haw.shc.Room.RaumActivity;
import de.haw.shc.Szenario.SzenarioActivity;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity {

    private Map<String,List<Device>> roomMap;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

        XML_Reader xml_reader = XML_Reader.createXMLReader(getResources());
        roomMap = xml_reader.getRaumMap();

        Button btn_szenarion = (Button) findViewById(R.id.btn_szenario);
        btn_szenarion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 Intent intent = new Intent(MainActivity.this, SzenarioActivity.class);
                startActivity(intent);
            }
        });

        Button btn_room = (Button) findViewById(R.id.btn_room);
        btn_room.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, RaumActivity.class);
            startActivity(intent);
            }
        });


        Button btn_device = (Button)findViewById(R.id.btn_device);
        btn_device.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, DeviceActivity.class);
                startActivity(intent);
            }
        });


        final ToggleButton btn_off = (ToggleButton) findViewById(R.id.tgl_on_off);
        btn_off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               if(btn_off.isChecked()){
                  sign_to_all(true);
               } else {
                   sign_to_all(false);
                }
            }
        });

    }


    private void sign_to_all(boolean on_off){
        Iterator<Map.Entry<String, List<Device>>> iter_map = roomMap.entrySet().iterator();
        while(iter_map.hasNext()){
            Map.Entry<String, List<Device>> entry = iter_map.next();
            for(Device elem: entry.getValue()){
                if(on_off){
                    Action_Impl.getInstance().iniAction(elem.activate(), elem.getDeviceControll());
                }else
                    Action_Impl.getInstance().iniAction(elem.deactivate(), elem.getDeviceControll());
            }
        }
    }




	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);

		return true;
	}

}
